import 'package:flutter/material.dart';
import 'package:mall/components/image_network.dart';
import 'package:mall/core/models/store_index.dart';
import 'package:mall/components/clock_countdown.dart';
import 'package:mall/screens/product/product_page.dart';
import 'package:rotated_corner_decoration/rotated_corner_decoration.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../constants.dart';

/// 抢购
class HomeRush extends StatelessWidget {
  final List<Floor> rush;

  const HomeRush({
    Key key,
    @required this.rush,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 8.w,
      runSpacing: 8.w,
      children: List.generate(rush.length, (index) => _buildFloorItem(context, index)),
    );
  }

  Widget _buildFloorItem(BuildContext context, int index) {
    final floor = rush[index];
    return Wrap(
      spacing: 8.w,
      runSpacing: 8.w,
      children: List.generate(
        floor.productLinks.length,
        (index) => _buildItem(context, floor, index),
      ),
    );
  }

  Widget _buildItem(BuildContext context, Floor floor, int index) {
    final link = floor.productLinks[index];
    return GestureDetector(
      onTap: () => _onTapItem(context, link),
      child: Container(
        width: ((375 - 32) ~/ 3).w,
        height: 115.w,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(4.w),
        ),
        foregroundDecoration: RotatedCornerDecoration(
          geometry: BadgeGeometry(
            width: 20.w,
            height: 20.w,
            cornerRadius: 4.w,
            alignment: BadgeAlignment.topLeft,
          ),
          color: Theme.of(context).primaryColor,
          textSpan: TextSpan(
            text: "抢",
            style: TextStyle(fontSize: 8.sp),
          ),
        ),
        child: Column(
          children: [
            // 图片
            ClipRRect(
              borderRadius: BorderRadius.vertical(
                top: Radius.circular(4.w),
              ),
              child: ImageNetwork(
                url: "${link.image}",
                width: ((375 - 32) ~/ 3).w,
                height: ((375 - 32) ~/ 3 - 20).w,
              ),
            ),
            Spacer(),
            // 倒计时
            Align(
              alignment: Alignment.bottomRight,
              child: Padding(
                padding: EdgeInsets.only(right: 4.w, bottom: 4.w),
                child: ClockCountdown(endTime: link.endTime),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _onTapItem(BuildContext context, Link link) {
    final uri = Uri.tryParse("$api_base_url${link.link}");
    debugPrint("$uri");
    final mId = int.tryParse("${uri?.pathSegments?.last}");
    final r = int.tryParse("${uri?.queryParameters["r"]}");
    Navigator.of(context).pushNamed(
      ProductPage.routeName,
      arguments: ProductPageArgs(null, null, mId, r),
    );
  }
}
